package com.future.mayi.activity;

import android.content.Context;
import android.content.Intent;
import android.text.TextUtils;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.future.baselib.entity.DefaultResponse;
import com.future.baselib.utils.ApiCst;
import com.future.baselib.utils.CommonUtils;
import com.future.baselib.utils.HttpRequest;
import com.future.mayi.R;
import com.future.mayi.app.App;
import com.future.mayi.model.BankModel;
import com.future.mayi.response.BankListResponse;
import com.bigkoo.pickerview.OptionsPickerView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;


public class JimaiPayActivity extends BaseTitleActivity {
    @BindView(R.id.tv_pay_name)
    TextView tvPayName;
    @BindView(R.id.tv_label_1)
    TextView tvLabel1;
    @BindView(R.id.edt_label_1)
    EditText edtLabel1;
    @BindView(R.id.ll_1)
    LinearLayout ll1;
    @BindView(R.id.view_1)
    View view1;
    @BindView(R.id.tv_label_2)
    TextView tvLabel2;
    @BindView(R.id.edt_label_2)
    EditText edtLabel2;
    @BindView(R.id.ll_2)
    LinearLayout ll2;
    @BindView(R.id.view_2)
    View view2;
    @BindView(R.id.tv_enter)
    TextView tvEnter;
    @BindView(R.id.tv_bank_name)
    TextView tvBankName;
    @BindView(R.id.ll_3)
    LinearLayout ll3;
    @BindView(R.id.view_3)
    View view3;


    private int position;
    private String orderGoodsId;

    public static void startActivity(Context context, String orderGoodsId) {
        context.startActivity(new Intent(context, JimaiPayActivity.class).putExtra("orderGoodsId", orderGoodsId));
    }

    @Override
    protected void initTopBar() {
        initBack();
        getTitleText().setText("完善结款信息");
    }

    @Override
    protected void initLayout() {
        setContentView(R.layout.activity_jimai_pay);
    }

    @Override
    protected void initView() {

    }


    private OptionsPickerView payDialog;
    private List<String> payList;

    @Override
    protected void initDates() {
        orderGoodsId = getIntent().getStringExtra("orderGoodsId");
        if (payDialog == null) {
            payList = new ArrayList<>();
            payList.add("充值到钱包");
            payList.add("支付宝");
            payList.add("银行卡");
            payDialog = new OptionsPickerView.Builder(JimaiPayActivity.this, new OptionsPickerView.OnOptionsSelectListener() {
                @Override
                public void onOptionsSelect(int options1, int options2, int options3, View v) {
                    dissmissLoadingDialog();
                    selectPay(options1);
                }
            }).setCancelColor(getResources().getColor(R.color.main_text_color))
                    .setSubmitColor(getResources().getColor(R.color.main_color))
                    .setContentTextSize(20)
                    .setTitleText("请选择结款方式")
                    .build();
        }
        payDialog.setPicker(payList);
        selectPay(0);
    }


    //0-用户钱包转账 1-支付宝 2-银行转账
    private void selectPay(int position) {
        this.position = position;
        if (position == 0) {
            ll1.setVisibility(View.GONE);
            ll2.setVisibility(View.GONE);
            ll3.setVisibility(View.GONE);
            view1.setVisibility(View.GONE);
            view2.setVisibility(View.GONE);
            view3.setVisibility(View.GONE);
            tvPayName.setText("充值到钱包");
        } else if (position == 1) {
            ll1.setVisibility(View.VISIBLE);
            ll2.setVisibility(View.VISIBLE);
            ll3.setVisibility(View.GONE);
            view1.setVisibility(View.VISIBLE);
            view2.setVisibility(View.VISIBLE);
            view3.setVisibility(View.GONE);
            tvPayName.setText("支付宝");
            tvLabel1.setText("支付宝账号");
            tvLabel1.setInputType(EditorInfo.TYPE_CLASS_TEXT);
            tvLabel2.setText("支付宝实名");
            edtLabel1.setHint("请填写支付宝账号");
            edtLabel2.setHint("请填写支付宝实名");
            edtLabel1.setText("");
            edtLabel2.setText("");
        } else if (position == 2) {
            ll1.setVisibility(View.VISIBLE);
            ll2.setVisibility(View.VISIBLE);
            ll3.setVisibility(View.VISIBLE);
            view1.setVisibility(View.VISIBLE);
            view2.setVisibility(View.VISIBLE);
            view3.setVisibility(View.VISIBLE);
            tvPayName.setText("银行卡");
            tvLabel1.setText("银行卡卡号");
            tvLabel1.setInputType(EditorInfo.TYPE_CLASS_NUMBER);
            edtLabel1.setHint("请填写银行卡卡号");
            tvLabel2.setText("银行卡开户名");
            edtLabel2.setHint("请填写银行卡开户名");
            edtLabel1.setText("");
            edtLabel2.setText("");
            tvBankName.setText("");
            getBankList();
        }
    }


    private boolean enter() {
        if (position == 1) {
            if (TextUtils.isEmpty(edtLabel1.getText())) {
                toast(edtLabel1.getHint().toString());
                return false;
            }
            if (TextUtils.isEmpty(edtLabel2.getText())) {
                toast(edtLabel2.getHint().toString());
                return false;
            }
        } else if (position == 2) {
            if (TextUtils.isEmpty(tvBankName.getText()) || tvBankName.getTag() == null) {
                toast(tvBankName.getHint().toString());
                return false;
            }

            if (TextUtils.isEmpty(edtLabel1.getText())) {
                toast(edtLabel1.getHint().toString());
                return false;
            }
            if (TextUtils.isEmpty(edtLabel2.getText())) {
                toast(edtLabel2.getHint().toString());
                return false;
            }

        }
        //0-支付宝 1-银行转账 2-用户钱包转账
        int position = 0;
        if (this.position == 0) position = 2;
        else if (this.position == 1) position = 0;
        else if (this.position == 2) position = 1;

        new HttpRequest<DefaultResponse>()
                .with(this).setApiCode(ApiCst.addPayInfo)
                .addParam("token", App.getToken(mContext))
                .addParam("order_goods_id", orderGoodsId)
                .addParam("type", position)
                .addParam("bank_id", position == 1 ? ((String) tvBankName.getTag()) : null)//银行ID
                .addParam("real_name", edtLabel2.getText().toString())//银行实名或支付宝实名
                .addParam("account_number", edtLabel1.getText().toString())//支付宝账号或银行账号
                .setListener(new HttpRequest.OnNetworkListener<DefaultResponse>() {
                    @Override
                    public void onSuccess(DefaultResponse response) {
                        toast(response.message);
                        finish();
                    }

                    @Override
                    public void onFail(String message, DefaultResponse response) {
                        toast(message);
                        tvEnter.setEnabled(true);
                    }
                }).start(new DefaultResponse());
        return true;
    }

    @Override
    protected void initListener() {

    }

    public List<BankModel> bankModels;
    private List<String> bankList;
    private OptionsPickerView bankDialog;

    public void getBankList() {
        if (bankModels != null && bankModels.size() > 0) return;
        new HttpRequest<BankListResponse>()
                .with(this).setApiCode(ApiCst.getBankList)
                .addParam("token", App.getToken(mContext))
                .setListener(new HttpRequest.OnNetworkListener<BankListResponse>() {
                    @Override
                    public void onSuccess(BankListResponse response) {
//                        bankModels = response.list;
                        if (bankModels == null || bankModels.size() == 0) {
                            bankList = new ArrayList<>(1);
                        } else {
                            bankList = new ArrayList<>(bankModels.size());
                            for (BankModel bankModel : bankModels) {
                                bankList.add(bankModel.getBank_name());
                            }
                        }
                        if (bankDialog == null) {
                            bankDialog = new OptionsPickerView.Builder(JimaiPayActivity.this, new OptionsPickerView.OnOptionsSelectListener() {
                                @Override
                                public void onOptionsSelect(int options1, int options2, int options3, View v) {
                                    dissmissLoadingDialog();
                                    tvBankName.setTag(String.valueOf(bankModels.get(options1).getBank_id()));
                                    tvBankName.setText(bankList.get(options1));
                                }
                            }).setCancelColor(getResources().getColor(R.color.main_text_color))
                                    .setSubmitColor(getResources().getColor(R.color.main_color))
                                    .setContentTextSize(20)
                                    .setTitleText("请选择银行")
                                    .build();
                        }
                        bankDialog.setPicker(bankList);
                    }

                    @Override
                    public void onFail(String message, BankListResponse response) {
                        toast(message);
                    }
                }).start(new BankListResponse());

    }


    @OnClick({R.id.tv_pay_name, R.id.tv_bank_name, R.id.tv_enter})
    public void onViewClicked(View view) {
        CommonUtils.hideKeyboard(null, this);
        switch (view.getId()) {
            case R.id.tv_pay_name:
                payDialog.show();
                break;
            case R.id.tv_bank_name:
                if (bankModels == null) {
                    getBankList();
                    toast("正在获取银行列表");
                    return;
                }
                if (bankDialog == null) return;
                bankDialog.show();
                break;
            case R.id.tv_enter:
                tvEnter.setEnabled(false);
                if (!enter()) tvEnter.setEnabled(true);
                break;
        }
    }


}
